package com.example.sneakerappserver.service;

import com.example.sneakerappserver.pojo.Category;
import com.example.sneakerappserver.pojo.Goods;
import com.example.sneakerappserver.repository.CategoryRepository;
import com.example.sneakerappserver.repository.GoodsRepository;
import com.example.sneakerappserver.util.ImageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsService implements IGoodsService{

    @Autowired
    GoodsRepository goodsRepository;

    @Autowired
    CategoryRepository categoryRepository;

    public Goods saveGoodsWithImage(String name, String desc, double price, String imagePath) throws IOException {
        Goods goods = new Goods();
        goods.setName(name);
        goods.setDes(desc);
        goods.setPrice(price);
        goods.setImageData(ImageUtils.imageToBytes(imagePath)); // 将图片转换为字节数组
        return goodsRepository.save(goods);
    }


    public Goods createGoods(String name, String desc, double price, long categoryId, byte[] imageData) {
        // 查找 Category
        Category category = categoryRepository.findById(categoryId)
                .orElseThrow(() -> new RuntimeException("Category not found"));

        // 创建 Goods
        Goods goods = new Goods();
        goods.setName(name);
        goods.setDes(desc);
        goods.setPrice(price);
        goods.setCategory(category);
        goods.setImageData(imageData);

        return goodsRepository.save(goods);
    }

    @Override
    public List<Goods> queryAllGoods() throws IOException {
//        // 先插入数据
//        ArrayList<Category> catList = Category.getDefaultList();
//
//        categoryRepository.saveAll(catList);    // 保存所有Category
//        Category shoe = categoryRepository.findByCategoryId(1L);
//        for(int i=0; i<Goods.mNameArray.length;i++){
//            // 插入所有goods
//            Goods goods = new Goods();
//            goods.setCategory(shoe);
//            goods.setName(Goods.mNameArray[i]);
//            goods.setDes(Goods.mDescArray[i]);
//            goods.setPrice(Goods.mPriceArray[i]);
//            goods.setImageData(ImageUtils.imageToBytes(Goods.mImagePath[i]));
//            goods.setCapcity(30);   // 库存
//            goods.setStatus(0); // 状态
//            goodsRepository.save(goods);
//        }

//        List<Goods> allGoods = goodsRepository.findAll();
//        for(int i=0; i<Goods.mNameArray.length;i++){
//            // 插入所有goods
//            Goods goods = allGoods.get(i);
////            Goods goods = new Goods();
////            goods.setCategory(shoe);
//
//            goods.setImageData(ImageUtils.imageToBytes(Goods.mImagePath[i]));
//
//            goodsRepository.save(goods);
//        }

        return (List<Goods>) goodsRepository.findAll(); // category
    }

    @Override
    public Goods queryByGoodsId(Long goodsId) {
        return goodsRepository.findByGoodsId(goodsId);  // 查找商品
    }

    @Override
    public Goods update(Goods goods) {
        return goodsRepository.save(goods);//更新库存
    }


}
